﻿namespace nCodeGeneratingTool
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;

    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;

    public class CommandData
    {
        #region Constructors

        public CommandData(int id, string displayName, string command, string xsltfile)
        {
            this.Id = id;
            this.DisplayName = displayName;
            this.CommandName = command;
            this.XsltFile = xsltfile;
        }

        #endregion Constructors

        #region Properties

        public string CommandName
        {
            get; set;
        }

        public string DisplayName
        {
            get; set;
        }

        public int Id
        {
            get; set;
        }

        public string XsltFile
        {
            get; set;
        }

        #endregion Properties
    }

    [Guid("5089420B-D6B9-4118-8A28-482390882CC4")]
    public class NCSharpCodeGenerator : IVsSingleFileGenerator
    {
        #region Properties

        private string endComment
        {
            get; set;
        }

        private string extenstion
        {
            get; set;
        }

        private string startComment
        {
            get; set;
        }

        #endregion Properties

        #region Methods

        public int DefaultExtension(out string pbstrDefaultExtension)
        {
            pbstrDefaultExtension = extenstion;
            return pbstrDefaultExtension.Length;
        }

        public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] rgbOutputFileContents,
            out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
        {
            XMLProperties propCollection = readProperties(wszInputFilePath);
            SetFileExtension(wszInputFilePath);
            CodegGenHelper helper = new CodegGenHelper();
            byte[] bytes = Encoding.UTF8.GetBytes(startComment + "Auto Generated by Nipesh Shah's Auto Generating Free Tool\n" + endComment + helper.GenerateOutputfile(wszInputFilePath, bstrInputFileContents, wszDefaultNamespace, startComment, endComment, propCollection));
            int length = bytes.Length;
            //int lineCount = bstrInputFileContents.Split('\n').Length;
            //byte[] bytes = Encoding.UTF8.GetBytes(lineCount.ToString());
            //int length = bytes.Length;
            rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(length);
            Marshal.Copy(bytes, 0, rgbOutputFileContents[0], length);
            pcbOutput = (uint)length;
            return 0;
        }

        private XMLProperties readProperties(string wszInputFilePath)
        {
            string DirectoryPath = Path.GetDirectoryName(wszInputFilePath);
            string ConfigFilePath = DirectoryPath + "\\ncgConfig.xml";
            if (File.Exists(ConfigFilePath))
            {
                XMLProperties xml = new XMLProperties(ConfigFilePath);
                return xml;
            }
            else
            {
                throw new Exception("Config File not available at location " + DirectoryPath);
            }
        }

        private void SetFileExtension(string wszInputFilePath)
        {
            extenstion = Path.GetExtension(wszInputFilePath);
            switch (extenstion.ToLower())
            {
                case ".cs":
                    startComment = "//";
                    endComment = string.Empty;
                    break;
                case ".sql":
                    startComment = "--";
                    endComment = string.Empty;
                    break;
                case ".aspx":
                    startComment = "<%--";
                    endComment = "--%>";
                    break;
                case ".ascx":
                    startComment = "<%--";
                    endComment = "--%>";
                    break;
                default:
                    startComment = string.Empty;
                    endComment = string.Empty;
                    break;
            }
        }

        #endregion Methods

        #region Other

        //[ComRegisterFunction]
        //public static void RegisterClass(Type t)
        //{
        //    GuidAttribute guidAttribute = getGuidAttribute(t);
        //    CustomToolAttribute customToolAttribute = getCustomToolAttribute(t);
        //    using (RegistryKey key = Registry.LocalMachine.CreateSubKey(
        //      GetKeyName(CSharpCategoryGuid, customToolAttribute.Name)))
        //    {
        //        key.SetValue("", customToolAttribute.Description);
        //        key.SetValue("CLSID", "{" + guidAttribute.Value + "}");
        //        key.SetValue("GeneratesDesignTimeSource", 1);
        //    }
        //}
        //[ComUnregisterFunction]
        //public static void UnregisterClass(Type t)
        //{
        //    CustomToolAttribute customToolAttribute = getCustomToolAttribute(t);
        //    Registry.LocalMachine.DeleteSubKey(GetKeyName(
        //      CSharpCategoryGuid, customToolAttribute.Name), false);
        //}

        #endregion Other
    }
}